package com.ht.system.controller;

import com.ht.common.core.convert.EntityDtoConverter;
import com.ht.common.core.convert.EntitySelectConverter;
import com.ht.common.core.domain.R;
import com.ht.common.core.domain.vo.EntitySelectVo;
import com.ht.common.core.web.controller.BaseController;
import com.ht.common.core.web.domain.AjaxResult;
import com.ht.common.core.web.page.TableDataInfo;
import com.ht.common.log.annotation.Log;
import com.ht.common.log.enums.BusinessType;
import com.ht.common.security.annotation.InnerAuth;
import com.ht.common.security.annotation.RequiresLogin;
import com.ht.common.security.annotation.RequiresPermissions;
import com.ht.common.security.utils.SecurityUtils;
import com.ht.system.api.dto.ChannelInfoDto;
import com.ht.system.domain.BaseFieldInfo;
import com.ht.system.domain.ChannelInfo;
import com.ht.system.service.IChannelInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 通道信息Controller
 *
 * @author zmx
 * @date 2025-03-22
 */
 @Api(tags = "通道信息接口")
@RestController
@RequestMapping("/channelInfo")
public class ChannelInfoController extends BaseController
{
    @Autowired
    private IChannelInfoService channelInfoService;

    /**
     * 查询通道信息列表
     */
    @ApiOperation("列表,权限:system:ChannelInfo:list")
    @RequiresPermissions("system:ChannelInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(ChannelInfo channelInfo)
    {
        startPage();
        List<ChannelInfo> list = channelInfoService.selectChannelInfoList(channelInfo);
        return getDataTable(list);
    }

    @ApiOperation("下来列表不分页")
    @RequiresLogin
    @GetMapping("/findList/{fieldCode}")
    public AjaxResult findList(@PathVariable("fieldCode") String fieldCode)
    {
        ChannelInfo queryCi=new ChannelInfo();
        queryCi.setFieldCode(fieldCode);
        List<ChannelInfo> list = channelInfoService.selectChannelInfoList(queryCi);
        List<EntitySelectVo> result= EntitySelectConverter.convert(
                list,
                ChannelInfo::getChannelCode,
                ChannelInfo::getChannelName
        );
        return AjaxResult.success(result);
    }



    /**
     * 获取通道信息详细信息
     */
    @ApiOperation("详细信息,权限:system:ChannelInfo:list")
    @RequiresPermissions("system:ChannelInfo:list")
    @GetMapping(value = "/getInfo/{channelCode}")
    public AjaxResult getInfo(@PathVariable("channelCode") String channelCode)
    {
        return AjaxResult.success(channelInfoService.selectChannelInfoByChannelCode(channelCode));
    }

    /**
     * 新增通道信息
     */
    @ApiOperation("新增,权限:system:ChannelInfo:add")
    @RequiresPermissions("system:ChannelInfo:add")
    @Log(title = "通道信息", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ChannelInfo channelInfo)
    {
    			channelInfo.setCreateTime(new Date());
    			channelInfo.setCreateBy(SecurityUtils.getNickName());
        return toAjax(channelInfoService.insertChannelInfo(channelInfo));
    }

    /**
     * 修改通道信息
     */
    @ApiOperation("修改,权限:system:ChannelInfo:edit")
    @RequiresPermissions("system:ChannelInfo:edit")
    @Log(title = "通道信息", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ChannelInfo channelInfo)
    {
        return toAjax(channelInfoService.updateChannelInfo(channelInfo));
    }

    /**
     * 删除通道信息
     */
    @ApiOperation("删除,权限:system:ChannelInfo:remove")
    @RequiresPermissions("system:ChannelInfo:remove")
    @Log(title = "通道信息", businessType = BusinessType.DELETE)
	@GetMapping("remove/{channelCodes}")
    public AjaxResult remove(@PathVariable("channelCodes") String[] channelCodes)
    {
        return toAjax(channelInfoService.deleteChannelInfoByChannelCodes(channelCodes));
    }


    @InnerAuth
    @PostMapping("findByChannelCode/{channelCode}")
    public R<ChannelInfoDto> findByChannelCode(@PathVariable("channelCode") String channelCode)
    {
        ChannelInfo channelInfo=  channelInfoService.selectChannelInfoByChannelCode(channelCode);
        return R.ok(EntityDtoConverter.convert(channelInfo,ChannelInfoDto.class));
    }

    @InnerAuth
    @PostMapping("findByFieldCode/{fieldCode}")
    public R<List<ChannelInfoDto>> findListByCustomsCode(@PathVariable("fieldCode") String fieldCode)
    {
        List<ChannelInfo> list=  channelInfoService.selectListByFieldCode(fieldCode);
        return R.ok(EntityDtoConverter.convertList(Collections.singleton(list),ChannelInfoDto.class));
    }
}
